package com.linghang.lhonline.controller;


import com.linghang.lhonline.Util.ResultVOUtil;
import com.linghang.lhonline.Util.HomeworkSortUtil;
import com.linghang.lhonline.VO.ResultVO;
import com.linghang.lhonline.dataobject.Homework;
import com.linghang.lhonline.dataobject.Mission;
import com.linghang.lhonline.service.HomeworkService;
import com.linghang.lhonline.service.MissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/homework")
@Slf4j
public class HomeworkController {

    private HomeworkService homeworkService;
    private MissionService missionService;

    @Autowired
    public HomeworkController(HomeworkService homeworkService, MissionService missionService) {
        this.homeworkService = homeworkService;
        this.missionService = missionService;
    }

    @GetMapping("/list")
    public ResultVO list(@RequestParam("student_id") String studentId) {

        List<Homework> homeworkList = homeworkService.findByStudentId(studentId);

        //按照时间进行排序
        HomeworkSortUtil homeworkSortUtil = new HomeworkSortUtil();

        homeworkList.sort(homeworkSortUtil);

        return ResultVOUtil.success("获取自己所有的作业", homeworkList);
    }

    @PostMapping("/delete")
    public ResultVO delete(@RequestParam("homework_id") Integer homeworkId) {
        Homework homework = homeworkService.findByHomeworkId(homeworkId);

        Mission mission = missionService.findByMissionId(homework.getMissionId());

        if (mission.getEndTime().getTime() < System.currentTimeMillis()) {
            return ResultVOUtil.error("该任务已完结，无法删除作业");
        }

        homeworkService.delete(homework);

        return ResultVOUtil.success("删除成功");
    }
}
